import java.util.Random;

/**
 * @author: gao
 * @create-date: 2025/3/8 15:55
 */

public class Sort {
    public void HeapSort(int[] array) {
        CreateHeap(array);
        int end = array.length - 1;
        while(end > 0){
            swap(array,0,end);
            ShiftDown(array,0,end);
            end--;
        }
    }

    private void CreateHeap(int[] array) {
        for (int parent = (array.length - 1 - 1) / 2; parent >= 0; parent--) {
            ShiftDown(array, parent, array.length);
        }
    }

    private void ShiftDown(int[] array, int parent, int length) {
        int child = 2 * parent + 1;
        while (child < length) {
            if (child + 1 < length && array[child] < array[child + 1]) {
                child++;
            }
            if (array[child] > array[parent]) {
                swap(array,parent,child);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    private void swap(int[] array,int i,int j){
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public void DisOrder(int[] array){
        Random ran = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = ran.nextInt(1,100);
        }
    }
}